package com.ooono.factorytest.items;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.text.format.Formatter;
import android.widget.TextView;

import com.ooono.factorytest.R;
import com.ooono.factorytest.base.BaseActivity;

import java.io.File;
import java.lang.reflect.Method;

public class StorageTestActivity extends BaseActivity {

    private static final String TAG = "StorageTestActivity";

    public static final String KEY = "storage";

    private TextView emmcAvailableValueTv;
    private TextView emmcTotalCapacityValueTv;
    private TextView flashAvailableValueTv;
    private TextView flashTotalCapacityValueTv;

    private StorageManager mStorageManager;

    private BroadcastReceiver mMountPointReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String mountPoint = null;
            StorageVolume volume = null;
            Uri mountPointUri = intent.getData();
            if (mountPointUri != null) {
                mountPoint = mountPointUri.getPath();
            }

            if (mountPoint == null || mountPointUri == null) {
                return;
            }

            if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {

            } else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {

            } else if (Intent.ACTION_MEDIA_EJECT.equals(action)) {

            }

            setEmmcFlashValue();
        }
    };

    @Override
    public int getTestTitle() {
        return R.string.storage_test_prompt;
    }

    @Override
    public String getTestKey() {
        return KEY;
    }

    @Override
    public int getContentLayout() {
        return R.layout.content_storage_test;
    }

    @Override
    protected void onResume() {
        super.onResume();
        setEmmcFlashValue();

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
        intentFilter.addDataScheme("file");
        registerReceiver(mMountPointReceiver, intentFilter);
    }

    @Override
    public void initViews() {
        emmcAvailableValueTv = findViewById(R.id.tv_emmc_available_value);
        emmcTotalCapacityValueTv = findViewById(R.id.tv_emmc_total_capacity_value);
        flashAvailableValueTv = findViewById(R.id.tv_flash_available_value);
        flashTotalCapacityValueTv = findViewById(R.id.tv_flash_total_capacity_value);
    }

    private void setEmmcFlashValue() {
        mStorageManager = getSystemService(StorageManager.class);
        // init T-FLASH CARD
        flashAvailableValueTv.setText(getResources().getString(R.string.storage_test_flash_no));
        flashTotalCapacityValueTv.setText("");

        // init emmc
        emmcAvailableValueTv.setText(getResources().getString(R.string.storage_test_emmc_no));
        emmcTotalCapacityValueTv.setText("");

        StorageVolume[] storageVolumeList = getVolumeList(mStorageManager);
        if (storageVolumeList != null) {
            for (StorageVolume volume : storageVolumeList) {
                // T-Flash detected
                if (volume.isRemovable()) {
                    flashAvailableValueTv.setText(getResources().getString(R.string.storage_test_flash_yes));
                    File f = getPathFile(volume);
                    if (f != null) {
                        flashTotalCapacityValueTv.setText(Formatter.formatFileSize(this, f.getTotalSpace()));
                    }
                } else {
                    emmcAvailableValueTv.setText(getResources().getString(R.string.storage_test_emmc_yes));
                    File f = getPathFile(volume);
                    if (f != null) {
                        emmcTotalCapacityValueTv.setText(Formatter.formatFileSize(this, f.getTotalSpace()));
                    }
                }
            }
        }
    }


    public static StorageVolume[] getVolumeList(StorageManager storageManager) {
        try {
            Class clz = StorageManager.class;
            Method getVolumeList = clz.getMethod("getVolumeList", new Class[0]);
            StorageVolume[] result = (StorageVolume[]) getVolumeList.invoke(storageManager, new Object[]{});
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static File getPathFile(StorageVolume volume) {
        try {
            Class clz = StorageVolume.class;
            Method getPathFile = clz.getMethod("getPathFile", new Class[0]);
            File f = (File) getPathFile.invoke(volume, new Object[]{});
            return f;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(mMountPointReceiver);
        super.onDestroy();
    }

}